<template>
  <div class="member-order">
    <!-- tabs -->
    <xtx-tabs v-model="activeName" @tab-click="change">
      <xtx-tabs-panel
        v-for="item in orderStatus"
        :label="item.label"
        :key="item.name"
        :name="item.name"
      />
    </xtx-tabs>
    <!-- 列表 -->
    <div class="order-list">
      <div v-if="loading" class="loading" />
      <div class="none" v-if="!loading && orderList.length === 0">暂无数据</div>
      <order-item
        @on-cancel="cancel(item)"
        @on-delete="del(item)"
        @on-confirm="onConfirmOrder(item)"
        @on-logistics="logistics(item)"
        v-for="item in orderList"
        :key="item.id"
        :order="item"
      />
      <!-- 分页 -->
      <xtx-pagination
        v-if="total > 0"
        @current-change="requestParams.page = $event"
        :total="total"
        :page-size="requestParams.pageSize"
        :current-page="requestParams.page"
      />
    </div>
    <!-- 取消 dialog -->
    <order-cancel ref="cancelCom" />
    <!-- 查看物流 dialog -->
    <OrderLogistics ref="logisticsOrderCom" />
  </div>
</template>

<script setup>
import OrderLogistics from './components/order-logistics'
import { confirmOrder, findOrderList } from '@/api/order'
import { watch } from '@vue/runtime-core'
import OrderItem from './components/order-item.vue'
import OrderCancel from './components/order-cancel.vue'
import { deleteCart } from '@/api/cart'
import Confirm from '@/components/library/Confirm'
import Message from '@/components/library/Message'
const { orderStatus } = require('@/api/constants')
const { ref, reactive } = require('@vue/reactivity')

const activeName = ref('all')

// 接收父组件传递的标识和下标
const change = ({ index }) => {
  requestParams.orderState = index
}

const logisticsOrderCom = ref(null)
// 查看物流
const logistics = (item) => {
  logisticsOrderCom.value.open(item)
}

// 查询订单参数
const requestParams = reactive({
  page: 1,
  pageSize: 5,
  orderState: 0,
})
const total = ref(0)
const loading = ref(false)
// 订单列表
const orderList = ref([])

const init = () => {
  loading.value = true
  // 查询订单
  findOrderList(requestParams).then((data) => {
    orderList.value = data.result.items
    total.value = data.result.counts
    loading.value = false
  })
}
watch(
  requestParams,
  () => {
    init()
  },
  { immediate: true },
)

const cancelCom = ref(null)
const cancel = (order) => {
  // 打开模态框并将当前需要操作的数据传递过去
  cancelCom.value.open(order, init)
}

const del = async (order) => {
  await Confirm({ text: '您确认删除该条订单吗？' })
  await deleteCart([order.id])
  Message({ msg: '删除订单成功' })
  init()
}

const onConfirmOrder = async (order) => {
  await Confirm({ text: '您确认收到货吗？确认后货款将会打给卖家' })
  await confirmOrder(order.id)
  Message({ msg: '收货成功' })
  init()
}
</script>

<style scoped lang="less">
.order-list {
  padding: 20px;
  position: relative;
  background: white;
  min-height: 400px;
}
.loading {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  background: rgba(255, 255, 255, 0.9) url(~@/assets/images/loading.gif)
    no-repeat center;
}
.none {
  height: 400px;
  text-align: center;
  line-height: 400px;
  color: #999;
}
</style>
